package gameDirector;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
import gameManager.GameManager;
import gameDisplayer.GameDisplayer;

public class GameDirector extends JPanel implements ActionListener {
    private GameManager myGameManager;
    private GameDisplayer myGameDisplayer;
    private Timer time;
    private int trigger = 0;
    private boolean isGameStart = false;
    
    
    public GameDirector() {
       
        
        myGameManager = new GameManager();
        myGameDisplayer = new GameDisplayer(myGameManager);
        time = new Timer(10, this);
        time.start();
        
        
        
        //this.requestFocus();
        
        initializeGame();
    }
    
    public void actionPerformed(ActionEvent e){
        if (!myGameManager.isInitialized()) {
            
        }
        else {
            if (isGameStart) {
                //requestFocusInWindow();
                if (myGameManager.getCurrentLevel() != null) {
                    trigger++;
                    
                    myGameManager.update(trigger);
                    myGameDisplayer.paintGameObjects();
                   
                    int h = myGameManager.getPlayerCharacter().getHealthValue();
                    
                    //System.out.print(h);
                }
                
                if (isGameOver()) {
                    gameOver();
                }
            }
            else {
                //requestFocusInWindow();
                isGameStart = true;
                time.restart();
            }
        }  
} 
    
    public void initializeGame() {
        myGameDisplayer.displayStartupMenu();
        //myGameDisplayer.drawBackground(myGameManager.getBackgroundImage());
        
        time.start();
        //requestFocusInWindow();
        //addKeyListener(new AL());
    }
    
    public boolean isGameOver() {
        return (myGameManager.getPlayerCharacter().getHealthValue() <= 0
                || myGameManager.getInteractableObjectByName("EnemyStaticBoss") == null
                || myGameManager.getInteractableObjectByName("EnemyStaticBoss").isDying());
        //return false;
    }
    
    public void gameOver() {
        time.stop();
    }
    
    
}
